package com.itheima.reggie.controller;

import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.itheima.reggie.common.PageBean;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.util.ExcelUtils;
import org.apache.ibatis.annotations.Delete;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("dish")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private HttpSession session;
    /**
     * 分页条件查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @RequestMapping("/page")
    public R<PageBean> findPage(int page, int pageSize, String name){
        return dishService.findPage(page,pageSize,name);
    }

    /**
     * 新增菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> addDish(@RequestBody DishDto dishDto){

        //设置操作用户
        dishDto.setCreateUser((Long) session.getAttribute("employee"));
        dishDto.setUpdateUser((Long) session.getAttribute("employee"));

        return dishService.addDish(dishDto);
    }

    /**
     * 根据id或者菜品和口味数据回显前端
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> findDishDtoById(@PathVariable("id") long id){
        return dishService.findDishDtoById(id);
    }

    /**
     * 更新菜品和口味数据
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> updateDish(@RequestBody DishDto dishDto){
        dishDto.setUpdateUser((Long)session.getAttribute("employee"));
       return dishService.update(dishDto);
    }

    /**
     * 批量禁用菜品
     */
    @RequestMapping("/status/{status}")
    public R<String> updateDishStatus(@PathVariable("status") int status,Long[] ids){
        return dishService.disableDish(status,ids);
    }

    /**
     * 批量删除菜品
     */
    @DeleteMapping()
    public R<String> deleteDish(Long[] ids){
        return dishService.deleteDish(ids);
    }

    @Autowired(required = false)
    private DishMapper dishMapper;
    @RequestMapping("/test")
    public DishDto find(Long id){
       return dishMapper.findDishDto(id);
    }


    /**
     * 查询菜品列表
     * @param
     * @return
     */
    @RequestMapping("/list")
    public R<List<DishDto>> findDishListById(Long categoryId,Integer status){
        return dishService.findDishListById(categoryId,status);
    }

    @RequestMapping(value = "/exportExcel")
    public void exportExcel(long[] ids,HttpServletResponse response) throws IOException {
        ExcelUtils.downloadExcel(dishService.exportExcel(ids),"菜品表",response);
    }

//    @RequestMapping(value = "/dish.xlsx", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
//    @ResponseBody
//    byte[] testDown() throws IOException, InvalidFormatException {
//        Workbook workbook = dishService.exportExcel(ids);
//        ByteArrayOutputStream os = new ByteArrayOutputStream();
//        workbook.write(os);
//        byte[] bytes = os.toByteArray();
//        return bytes;
//    }

}
